package com.rcadmin.busi.web.dao;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.google.common.collect.Lists;
import com.rcadmin.busi.web.contant.LinkType;
import com.rcadmin.busi.web.ddl.WebBlock;
import com.rcadmin.common.contant.IsEff;
import com.rcadmin.core.jpa.BaseDao;
import com.rcadmin.util.SqlUtil;

/**
 * 内容信息DAO
 * 
 * @author tanson lam
 * @create 2016年10月26日
 */
@Component
public class BlockDao {
	@Resource
	private BaseDao baseDao;

	/**
	 * 查找单个内容信息
	 * 
	 * @param userId
	 * @param blockId
	 * @return
	 */
	public WebBlock findOne(Integer userId, Integer blockId) {
		StringBuilder query = new StringBuilder();
		query.append(" and id = ").append(blockId);
		query.append(" and user_id = ").append(userId);
		return baseDao.findOne(query.toString(), WebBlock.class);
	}

	/**
	 * 查询多个分组的属性块
	 * 
	 * @param userId
	 * @param groupKeys
	 * @return
	 */
	public List<WebBlock> listGroupBlock(Integer userId,
			List<Integer> groupKeyIds) {
		if (groupKeyIds.isEmpty())
			return Lists.newArrayList();

		StringBuilder query = new StringBuilder();
		query.append(" and user_id = ").append(userId);
		query.append(" and group_key_id in (")
				.append(SqlUtil.array2InCondition(groupKeyIds)).append(")");
		query.append(" and iseff = '").append(IsEff.EFFECTIVE).append("'");
		return baseDao.find(query.toString(), WebBlock.class);
	}

	/**
	 * 查询多个配置资源模板的内容
	 * 
	 * @param userId
	 * @param groupKeys
	 * @return
	 */
	public List<WebBlock> listResBlock(Integer userId, List<Integer> groupKeyIds) {
		if (groupKeyIds.isEmpty())
			return Lists.newArrayList();

		StringBuilder query = new StringBuilder();
		query.append(" and user_id = ").append(userId);
		query.append(" and group_key_id in (")
				.append(SqlUtil.array2InCondition(groupKeyIds)).append(")");
		query.append(" and iseff = '").append(IsEff.EFFECTIVE).append("'");
		query.append(" and link_type = '").append(LinkType.RES.getType())
				.append("'");

		return baseDao.find(query.toString(), WebBlock.class);
	}

	/**
	 * 查询多个分组的属性块
	 * 
	 * @param userId
	 * @param blockIds
	 * @return
	 */
	public List<WebBlock> listBlockIds(Integer userId, List<Integer> blockIds) {
		if (blockIds.isEmpty())
			return Lists.newArrayList();

		StringBuilder query = new StringBuilder();
		query.append(" and user_id = ").append(userId);
		query.append(" and  id in (")
				.append(SqlUtil.array2InCondition(blockIds)).append(")");
		query.append(" and iseff = '").append(IsEff.EFFECTIVE).append("'");
		return baseDao.find(query.toString(), WebBlock.class);
	}
}
